﻿using System;
using System.Data;
using System.Data.SqlClient;

namespace Module.QuanTriHeThong.ThietLapHeThong
{
    public class TaiKhoan_ChucVu_PhongBan : DBInteractionBase
    {
        public string ID { get; set; }
        public DateTime NgayBoNhiem { get; set; }
        public string TaiKhoan { get; set; }
        public string MaKhoa { get; set; }
        public string MaPhongBan { get; set; }
        public string MaChucVu { get; set; }
        public bool IsActive { get; set; }
        public DataTable SelectAll(TaiKhoan_ChucVu_PhongBan obj, int pageindex, int pagesize, out int recordcount)
        {
            var dt = new DataSet();
            try
            {
                using (var dbcmd = new SqlCommand("tb_ChucVu_PhongBan_SelectAll", GetConnection()))
                {
                    dbcmd.CommandType = CommandType.StoredProcedure;
                    dbcmd.Parameters.Add(new SqlParameter("@TaiKhoan", string.IsNullOrEmpty(obj.TaiKhoan) ? "" : obj.TaiKhoan));
                    dbcmd.Parameters.Add(new SqlParameter("@MaChucVu", string.IsNullOrEmpty(obj.MaChucVu) ? "" : obj.MaChucVu));
                    dbcmd.Parameters.Add(new SqlParameter("@MaPhongBan", string.IsNullOrEmpty(obj.MaPhongBan) ? "" : obj.MaPhongBan));
                    dbcmd.Parameters.Add(new SqlParameter("@PageIndex", pageindex));
                    dbcmd.Parameters.Add(new SqlParameter("@PageSize", pagesize));
                    dbcmd.Parameters.Add(new SqlParameter("@RecordCount", SqlDbType.Int, 50)
                    {
                        Direction = ParameterDirection.Output
                    });
                    var dtAdapter = new SqlDataAdapter(dbcmd);
                    dtAdapter.Fill(dt);
                    recordcount = Convert.ToInt32(dbcmd.Parameters["@RecordCount"].Value);
                    return dt.Tables[0];
                }
            }
            catch (Exception ex)
            {
                throw new Exception("tb_ChucVu_PhongBan_SelectAll: " + ex.Message);
            }
        }

        public bool PhanQuyenTaiKhoan(TaiKhoan_ChucVu_PhongBan obj)
        {
            try
            {
                using (var dbcmd = new SqlCommand("tb_ChucVu_PhongBan_PhanQuyenTaiKhoan", GetConnection()))
                {
                    dbcmd.CommandType = CommandType.StoredProcedure;
                    dbcmd.Parameters.Add(new SqlParameter("@TaiKhoan", obj.TaiKhoan));
                    dbcmd.Parameters.Add(new SqlParameter("@MaKhoa", obj.MaKhoa));
                    if (string.IsNullOrEmpty(obj.MaPhongBan))
                    {
                        dbcmd.Parameters.AddWithValue("@MaPhongBan", DBNull.Value);
                    }
                    else
                    {
                        dbcmd.Parameters.Add(new SqlParameter("@MaPhongBan", obj.MaPhongBan));
                    }
                    if (string.IsNullOrEmpty(obj.MaChucVu))
                    {
                        dbcmd.Parameters.AddWithValue("@MaChucVu", DBNull.Value);
                    }
                    else
                    {
                        dbcmd.Parameters.Add(new SqlParameter("@MaChucVu", obj.MaChucVu));
                    }
                    dbcmd.ExecuteNonQuery();
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public DataTable SelectById(string id)
        {
            var dt = new DataSet();
            try
            {
                using (var dbcmd = new SqlCommand("tb_ChucVu_PhongBan_SelectById", GetConnection()))
                {
                    dbcmd.CommandType = CommandType.StoredProcedure;
                    dbcmd.Parameters.Add(new SqlParameter("@ID", id));
                    var dtAdapter = new SqlDataAdapter(dbcmd);
                    dtAdapter.Fill(dt);
                    return dt.Tables[0];
                }
            }
            catch (Exception ex)
            {
                throw new Exception("tb_ChucVu_PhongBan_SelectById: " + ex.Message);
            }
        }

        public bool UpdateIsActive(string id)
        {
            try
            {
                using (var dbcmd = new SqlCommand("tb_ChucVu_PhongBan_UpdateIsActive", GetConnection()))
                {
                    dbcmd.CommandType = CommandType.StoredProcedure;
                    dbcmd.Parameters.Add(new SqlParameter("@ID", id));
                    dbcmd.ExecuteNonQuery();
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public bool DeleteById(string id)
        {
            try
            {
                using (var dbcmd = new SqlCommand("tb_ChucVu_PhongBan_DeleteById", GetConnection()))
                {
                    dbcmd.CommandType = CommandType.StoredProcedure;
                    dbcmd.Parameters.Add(new SqlParameter("@ID", id));
                    dbcmd.ExecuteNonQuery();
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
    }
}
